package com.fusion.common;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DateUtils {

	// 日期转成字符串
	public static String dateToString(Date date, String dateFormat) {
		String dateTimeString = null;
		try {
			SimpleDateFormat sDateFormat = new SimpleDateFormat(dateFormat);
			dateTimeString = sDateFormat.format(date);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return dateTimeString;
	}

	// 字符串转成日期
	public static Date stringToDate(String time, String dateFormat) {
		Date date = null;
		try {
			SimpleDateFormat sDateFormat = new SimpleDateFormat(dateFormat);
			date = sDateFormat.parse(time);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return date;
	}

	// 获取最大时间
	public static Date maxDate(String time) {
		Date date = null;
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			time = time + " 23:59:59";
			date = simpleDateFormat.parse(time);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return date;
	}

	// 获取最小时间
	public static Date minDate(String time) {
		Date date = null;
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			time = time + " 00:00:00";
			date = simpleDateFormat.parse(time);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return date;
	}

	// 格式化国际时间
	public static String utcTimeToString(String utcTime) {
		try {
			utcTime = utcTime.replace("Z", " UTC");
			SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd'T'HHmmss Z");
			Date parse = format.parse(utcTime);
			SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			utcTime = sf.format(parse);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return utcTime;
	}

	// 获取计算后的时间
	public static Date getTimeCalculate(Date time, int timeType, int nums, Boolean flag) {
		Calendar calendar = Calendar.getInstance();
		if (time == null) {
			time = new Date();
		}
		calendar.setTime(time);
		if (flag) {
			calendar.add(timeType, +nums);
		} else {
			calendar.add(timeType, -nums);
		}
		return calendar.getTime();
	}

	// 获取两个时间相差多少秒
	public static int differentSeconds(Date minDate, Date maxDate) {
		long startTime = minDate.getTime();
		long endTtime = maxDate.getTime();
		int time = (int) ((endTtime - startTime) / 1000);
		return time;
	}

	/**
	 * 描述: 获取当前时间之前的24个小时 创建人: 赵兴炎 日期: 2019年10月24日
	 */
	public Map<String, Object> findStatisticalHour() {
		Map<String, Object> hashMap = new HashMap<>();
		try {
			String minTimeString = null;
			String maxTimeString = null;
			String[] arr = new String[24];
			SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy-MM-dd HH:00:00");
			SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd");
			SimpleDateFormat simpleDateFormat3 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			SimpleDateFormat simpleDateFormat4 = new SimpleDateFormat("yyyy-MM-dd HH");
			Calendar nowTime = Calendar.getInstance();
			Date date = nowTime.getTime();
			String format = simpleDateFormat2.format(date);
			Date minStringDate = minDate(format);
			if (date.compareTo(minStringDate) > 0) {
				nowTime.add(Calendar.HOUR, 1);
				date = nowTime.getTime();
			}
			nowTime.setTime(date);
			String formatDate = simpleDateFormat1.format(date);
			arr[arr.length - 1] = formatDate;
			for (int i = 0; i < 24; i++) {
				nowTime.add(Calendar.HOUR, -1);
				formatDate = simpleDateFormat1.format(nowTime.getTime());
				if (i < 22 || i == 22) {
					arr[arr.length - 2 - i] = formatDate;
				} else {
					minTimeString = formatDate;
				}
			}
			maxTimeString = arr[arr.length - 2];
			Date minTimeDate = simpleDateFormat1.parse(minTimeString);
			Date maxTimeDate = simpleDateFormat3
					.parse(simpleDateFormat4.format(simpleDateFormat4.parse(maxTimeString)) + ":59:59");
			hashMap.put("x", arr);
			hashMap.put("minTimeDate", minTimeDate);
			hashMap.put("maxTimeDate", maxTimeDate);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return hashMap;
	}

	public static String[] getBeforeTime(int nums) {
		String[] arr = new String[nums];
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
		Calendar calendar = null;
		for (int i = 0; i < nums; i++) {
			calendar = Calendar.getInstance();
			calendar.add(Calendar.DAY_OF_MONTH, -i);
			arr[nums - 1 - i] = simpleDateFormat.format(calendar.getTime());
		}
		return arr;
	}

	public static List<String> getBetweenDates(Date startTime, Date endTime) {
		List<String> x = new ArrayList<String>();
		Calendar tempStart = Calendar.getInstance();
		tempStart.setTime(startTime);
		x.add(dateToString(startTime,"yyyy-MM-dd HH:mm:ss"));
		long startTimeLong = startTime.getTime();
		long endTimeLong = endTime.getTime();
		int timeNUms = 0;
		while (startTimeLong != endTimeLong) {
			timeNUms = timeNUms + 1;
			tempStart.add(Calendar.SECOND, +1);
			Date time = tempStart.getTime();
			String dateToString = dateToString(time,"yyyy-MM-dd HH:mm:ss");
			startTimeLong = time.getTime();
			x.add(dateToString);
		}
		return x;
	}

	/**
	 * 描述: 获取两个时间相差几天
	 */
	public static int differentDays(Date minDate, Date maxDate) {
		Calendar cal1 = Calendar.getInstance();
		cal1.setTime(minDate);
		Calendar cal2 = Calendar.getInstance();
		cal2.setTime(maxDate);
		int day1 = cal1.get(Calendar.DAY_OF_YEAR);
		int day2 = cal2.get(Calendar.DAY_OF_YEAR);
		int year1 = cal1.get(Calendar.YEAR);
		int year2 = cal2.get(Calendar.YEAR);
		if (year1 != year2) {
			int timeDistance = 0;
			for (int i = year1; i < year2; i++) {
				if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0) {
					timeDistance += 366;
				} else {
					timeDistance += 365;
				}
			}
			return timeDistance + (day2 - day1);
		} else {
			return day2 - day1;
		}
	}

}
